package de.godlike.webgui;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;

import de.godlike.services.JWebminService;
import de.godlike.services.JWebminServiceService;

public class JWebminBean {
	private JWebminService service;

	/**
	 * @return the service
	 */
	public synchronized JWebminService getService() {
		if (this.service == null) {
			Context initCtx;
			try {
				initCtx = new InitialContext();
				Context envCtx = (Context) initCtx.lookup("java:comp/env");
				String serviceUrl = (String) envCtx.lookup("JWebminServiceUrl");
				String serviceUser = (String) envCtx
						.lookup("JWebminServiceUser");
				String servicePw = (String) envCtx.lookup("JWebminServicePw");

				this.service = new JWebminServiceService(new URL(serviceUrl),
						new QName("http://services.godlike.de/",
								"JWebminServiceService"))
						.getJWebminServicePort();
				Map<String, Object> requestContext = ((BindingProvider) this.service)
						.getRequestContext();
				requestContext.put(BindingProvider.USERNAME_PROPERTY,
						serviceUser);
				requestContext
						.put(BindingProvider.PASSWORD_PROPERTY, servicePw);
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return this.service;
	}
}
